package com.ntd.study.modular.nacos;

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingFactory;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.listener.NamingEvent;

import java.util.Properties;

public class Nacos {

    /**
     * @author nkorange
     */
    public static void main(String[] args) throws NacosException {

        Properties properties = new Properties();
        properties.setProperty("serverAddr", System.getProperty("serverAddr"));
        properties.setProperty("namespace", System.getProperty("namespace"));

        NamingService naming = NamingFactory.createNamingService(properties);

        naming.registerInstance("common-service", "11.11.11.11", 8888, "TEST1");

        naming.registerInstance("common-service", "2.2.2.2", 9999, "DEFAULT");

        System.out.println(naming.getAllInstances("common-service"));

        naming.deregisterInstance("common-service", "2.2.2.2", 9999, "DEFAULT");

        System.out.println(naming.getAllInstances("common-service"));

        naming.subscribe("common-service", event -> {
            System.out.println(((NamingEvent) event).getServiceName());
            System.out.println(((NamingEvent) event).getInstances());
        });
    }
}
